package org.csu.mypetstore.web.Filter;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.tools.WebLog;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class RemoveItemFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        String account = req.getParameter("account");
        Account account1 = null;
        if(account == null)  account1 = (Account)((HttpServletRequest)req).getSession().getAttribute("account");
        if(account1 != null) account = account1.getUsername();
        String itemId = (String) ((HttpServletRequest)req).getSession().getAttribute("cartItem");
        if(itemId == null) itemId = req.getParameter("cartItem");

        if(account!=null&&itemId!=null)
        {
            WebLog.WriteWebLog(account+ "  removes "+ itemId + " from  cart");
        }

        if( account ==null && itemId != null)
        {
            WebLog.WriteWebLog("A user removes "+itemId + " from cart" );
        }

        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
